/******************************************************************************
 * Copyright (C) 2011 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.excel.officeexport.excel.tag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;

/**
 * 求和标签类.
 * 
 * @author 朱庆华
 * @since 1.0
 * @version 2005-3-16 朱庆华 新建
 */
public class TageSum implements ITageDeal {
    
    /** 日志管理类对象 */
    private final Logger loger = Logger.getLogger(TageSum.class);
    
    /**
     * 构造函数
     */
    public TageSum() {
        
    }
    
    /**
     * 设置求和标签值
     * 
     * @param sheet Excel sheet对象
     * @param dataSource 数据源
     * @param alTages 所有求和标签
     */
    @SuppressWarnings("unchecked")
    public void setValue(HSSFSheet sheet, Map<String, Object> dataSource, List<ExcelCell> alTages) {
        int iTagesSize = alTages.size();
        ExcelCell objExcelCell;
        String strTag;// 标签值< sum name=XX colname=A rownum=N>
        String strName;// 标签属性name值
        String strColName;// 标签属性colname值
        String strRowNum;// 标签属性rownum值
        int iRowNum = 0;
        int iLastRowNum = 0;
        int iRowWork; // iColWork;
        int iInterval; // 求和标签行号与起始计算行号之间的差值
        HSSFCell objCell;
        Object objVal;
        
        for (int i = 0; i < iTagesSize; i++) {
            objExcelCell = alTages.get(i);
            strTag = objExcelCell.getTagStr();
            strName = ExcelUtil.getKeyWord(strTag, "name");
            strColName = ExcelUtil.getKeyWord(strTag, "colname");
            strRowNum = ExcelUtil.getKeyWord(strTag, "rownum");
            
            objVal = dataSource.get(strName);
            
            if (objVal != null) {
                iLastRowNum = ((ArrayList<Object>) objVal).size();
                try {
                    iRowNum = Integer.parseInt(strRowNum);
                } catch (NumberFormatException ex) {
                    loger.error(ex.toString(), ex);
                    continue;
                }
                
                // 计算求和签行号与起始计算行号之间的差值
                iInterval = objExcelCell.getRowno() - iRowNum;
                
                if (iInterval < 0) {
                    // 求和标签和起始计算行行号相同或求和标签在起始计算行之上
                    iRowWork = objExcelCell.getRowno();
                } else {
                    if (objExcelCell.getRowno() <= (iRowNum + iLastRowNum)) {
                        iRowWork = iRowNum + iLastRowNum - 1 + (objExcelCell.getRowno() - iRowNum);
                    } else {
                        iRowWork = objExcelCell.getRowno();
                    }
                }
                
                objCell = sheet.createRow(iRowWork).createCell(objExcelCell.getCellno());
                // objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
                objCell.setCellStyle(objExcelCell.getStyle());
                
                // 设置求和公式
                objCell.setCellFormula("SUM(" + strColName + strRowNum + ":" + strColName + (iRowNum + iLastRowNum - 1)
                    + ")");
                
            } else {
                objCell = sheet.getRow(objExcelCell.getRowno()).createCell(objExcelCell.getCellno());
                objCell.setCellStyle(objExcelCell.getStyle());
                objCell.setCellValue("");
                continue;
            }
        }
    }
    
}
